package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.PinYinSortManager;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.UserPageInfoModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetUserByCatHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetUserByCatReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.ExpertListAdapter;
import com.hangzhou.xyj.ui.widget.MyLetterListView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by neo.duan on 2016/1/6.
 * 名家/名团 列表
 */
public class ExpertListActivity extends BaseActivity {
    public static final int DEFAULT_TAG = 0; //默认
    public static final int DIAN_DRAMA_TAG = 1; //点戏进入该页面


    private RecyclerView mRecyclerView;
    private MyLetterListView mLetterLv; //字母索引

    private HashMap<String, Integer> alphaIndexer;//存放存在的汉语拼音首字母和与之对应的列表位置
    private PinYinSortManager mSortManager;
    private List<UserModel> mDataList;

    private ExpertListAdapter mAdapter;
    private int mIntentTag;
    private String mTitle;
    private int mCategoryId;//剧种分类Id

    @Override
    public void initIntentData(Intent intent) {
        super.initIntentData(intent);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, DEFAULT_TAG);
        mTitle = intent.getStringExtra(DataKey.TITLE);
        mCategoryId = intent.getIntExtra(DataKey.INDEX, -1);
    }

    @Override
    public void initTop() {
        enableTitle(true, mTitle);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_more_expert);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_more_expert);
        mLetterLv = findView(R.id.lv_more_expert);
        //设置水平布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mRecyclerView.setAdapter(mAdapter = new ExpertListAdapter(mContext, mDataList));
        mAdapter.setIntentTag(mIntentTag);
    }

    @Override
    public void initDatas() {
        //获取剧种id
        getExpertList(mCategoryId);
    }

    @Override
    public void initListeners() {
        mLetterLv.setOnTouchingLetterChangedListener(new MyLetterListView.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                alphaIndexer = mSortManager.getAlphaIndexer();
                if (alphaIndexer.get(s) != null) {
                    int position = alphaIndexer.get(s);
                    mRecyclerView.smoothScrollToPosition(position);
                    mSortManager.handlerView(position);
                }
            }
        });
    }

    /**
     * 对数据排序
     */
    private void sortData(List<UserModel> userList) {
        if (userList == null) {
            userList = new ArrayList<>();
        }
        this.mDataList = userList;
        // 字母排序索引管理器
        mSortManager = new PinYinSortManager(this);
        //进行按字母排序
        List<Object> sortDatas = mSortManager.sortDatas(mDataList, "name");
        mDataList.clear();
        for (Object obj : sortDatas) {
            mDataList.add((UserModel) obj);
        }
    }

    /**
     * 向服务器请求数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/12 11:19
     */
    private void getExpertList(int categoryId) {
        GetUserByCatReq getUserByCatReq;
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_STAGE_EXPERT://戏台-名家 进入
                getUserByCatReq = new GetUserByCatReq(GetUserByCatReq.TYPE_FAMOUS_EXPERT, categoryId, Integer.MAX_VALUE, 1);
                break;
            case TAG_STAGE_TEAM://戏台-演员 进入
                getUserByCatReq = new GetUserByCatReq(GetUserByCatReq.TYPE_ACTOR, categoryId, Integer.MAX_VALUE, 1);
                break;
            case TAG_DRAMA_GROUP://发现-剧团
                getUserByCatReq = new GetUserByCatReq(GetUserByCatReq.TYPE_DRAMA_GROUP, categoryId, Integer.MAX_VALUE, 1);
                break;
            case TAG_DIAN_DRAMA_EXPERT://点戏台-名家点戏
            default:
                getUserByCatReq = new GetUserByCatReq(GetUserByCatReq.ByTypeEnum.type, 2, Integer.MAX_VALUE, 1);
                break;

        }
        HttpLoader.getInstance().sendRequest(new GetUserByCatHandler(getUserByCatReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<UserModel> list = ((UserPageInfoModel) model).getUserList();
                //对数据进行排序
                sortData(list);
                if (list.size() == 0) {
                    setNoDataTip(true, "暂无数据哦~");
                } else {
                    mAdapter.update(mDataList);
                }
            }
        }, 0));
    }
}
